package ace.cmp.spring.data.jdbc.querydsl.rsql.core;

import com.querydsl.core.types.Predicate;
import cz.jirutka.rsql.parser.RSQLParser;
import cz.jirutka.rsql.parser.ast.ComparisonOperator;
import cz.jirutka.rsql.parser.ast.RSQLOperators;
import java.util.Set;

/**
 * @author caspar
 * @date 2023/4/14 11:45
 */
public class RsqlQuerydslParser {
  public static final RsqlQuerydslParser INSTANCE = new RsqlQuerydslParser();

  private final RSQLParser parser;

  public RsqlQuerydslParser() {
    Set<ComparisonOperator> operators = RSQLOperators.defaultOperators();
    operators.add(RsqlQuerydslOperators.LIKE);
    operators.add(RsqlQuerydslOperators.LIKE_RIGHT);
    this.parser = new RSQLParser(operators);
  }

  public Predicate parse(String query, RsqlQuerydslVisitor visitor) {
    return this.parser.parse(query).accept(visitor);
  }
}
